auto_enums
A library for to allow multiple return types by automatically generated enum.
This crate is a procedural macro implementation of the features discussions in rust-lang/rfcs#2414. This idea is also known as "Anonymous sum types".
This library provides the following attribute macros:
-
#[auto_enum]
Parses syntax, creates the enum, inserts variants, and passes specified traits to
#[enum_derive]
. -
#[enum_derive]
Implements specified traits to the enum.
Usage
Add this to your Cargo.toml
:
[]
= "0.8"
Examples
#[auto_enum]
's basic feature is to wrap the value returned by the obvious
branches (match
, if
, return
, etc..) by an enum that implemented the
specified traits.
use auto_enum;
#[auto_enum]
generates code in two stages.
First, #[auto_enum]
will do the following.
- parses syntax
- creates the enum
- inserts variants
Code like this will be generated:
Next, #[enum_derive]
implements the specified traits.
Code like this will be generated
#[auto_enum]
can also parse nested arms/branches by using the #[nested]
attribute.
use auto_enum;
See documentation for more details.
Supported traits
#[enum_derive]
implements the supported traits and passes unsupported
traits to #[derive]
.
#[enum_derive]
supports many of the standard library traits and some popular
third-party libraries traits such as rayon, futures,
tokio, http_body. See documentation for a complete list of supported traits.
If you want to use traits that are not supported by #[enum_derive]
, you
can use another crate that provides derives macros, or
you can define derives macros yourself (derive_utils probably can help it).
Basic usage of #[enum_derive]
use enum_derive;
// `#[enum_derive]` implements `Iterator`, and `#[derive]` implements `Clone`.
Optional features
std
(enabled by default)- Enable to use
std
library's traits.
- Enable to use
ops
- Enable to use
[std|core]::ops
'sDeref
,DerefMut
,Index
,IndexMut
, andRangeBounds
traits.
- Enable to use
convert
- Enable to use
[std|core]::convert
'sAsRef
andAsMut
traits.
- Enable to use
fmt
- Enable to use
[std|core]::fmt
's traits other thanDebug
,Display
andWrite
.
- Enable to use
transpose_methods
- Enable to use
transpose*
methods.
- Enable to use
futures03
- Enable to use futures v0.3 traits.
futures01
- Enable to use futures v0.1 traits.
rayon
- Enable to use rayon traits.
serde
- Enable to use serde traits.
tokio1
- Enable to use tokio v1 traits.
tokio03
- Enable to use tokio v0.3 traits.
tokio02
- Enable to use tokio v0.2 traits.
tokio01
- Enable to use tokio v0.1 traits.
http_body1
- Enable to use http_body v1 traits.
coroutine_trait
- Enable to use
[std|core]::ops::Coroutine
trait. - Note that this feature is unstable and may cause incompatible changes between patch versions.
- Enable to use
fn_traits
- Enable to use
[std|core]::ops
'sFn
,FnMut
, andFnOnce
traits. - Note that this feature is unstable and may cause incompatible changes between patch versions.
- Enable to use
trusted_len
- Enable to use
[std|core]::iter::TrustedLen
trait. - Note that this feature is unstable and may cause incompatible changes between patch versions.
- Enable to use
type_analysis
feature
Analyze return type of function and let
binding.
Note that this feature is still experimental.
Examples:
use auto_enum;
// there is no need to specify std library's traits
Please be careful if you return another traits with the same name.
Related Projects
- derive_utils: A procedural macro helper for easily writing derives macros for enums.
- io-enum: #[derive(Read, Write, Seek, BufRead)] for enums.
- iter-enum: #[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, Extend)] for enums.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.